﻿namespace HRM.CORE
{
    using DevExpress.XtraEditors;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;

    public class xfmContractAdd : XtraForm
    {
        private IContainer components;
        public RowClickEventArgs m_RowClickEventArgs;
        private xucContractAdd ucAdd;

        public event AddedEventHander Added;

        public event UpdatedEventHander Updated;

        public xfmContractAdd()
        {
            this.components = null;
            this.InitializeComponent();
        }

        public xfmContractAdd(Actions Action)
        {
            this.components = null;
            this.InitializeComponent();
            this.Init();
            this.ucAdd.Status = Action;
            this.ucAdd.SetData(new HRM_CONTRACT().NewID());
            this.Text = ((SysOption.Language == "EN") ? "New " : "Th\x00eam ") + this.Text;
        }

        public xfmContractAdd(Actions Action, HRM_CONTRACT Item)
        {
            this.components = null;
            this.InitializeComponent();
            this.Init();
            this.ucAdd.Status = Action;
            if (Action == Actions.Update)
            {
                this.ucAdd.SetData(Item);
                this.Text = ((SysOption.Language == "EN") ? "Edit " : "Cập Nhật ") + this.Text;
            }
            else if (Action == Actions.Delete)
            {
                this.ucAdd.SetData(Item.ContractCode);
            }
        }

        public xfmContractAdd(Actions Action, string EmployeeCode)
        {
            this.components = null;
            this.InitializeComponent();
            this.Init();
            this.ucAdd.Status = Action;
            if (Action == Actions.Add)
            {
                HRM_CONTRACT hrm_contract = new HRM_CONTRACT();
                this.ucAdd.SetDefaultAdd(EmployeeCode);
                this.ucAdd.SetData(hrm_contract.NewID());
                this.Text = ((SysOption.Language == "EN") ? "Edit " : "Th\x00eam ") + this.Text;
            }
            else if (Action == Actions.Delete)
            {
            }
        }

        public void Delete()
        {
            this.ucAdd.Delete();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void Init()
        {
            this.ucAdd = new xucContractAdd();
            base.Controls.Add(this.ucAdd);
            this.ucAdd.CancelClick += new ButtonClickEventHander(this.ucAdd_CancelClick);
            this.ucAdd.Success += new xucContractAdd.SuccessEventHander(this.ucAdd_Success);
            this.Text = (SysOption.Language == "EN") ? "HRM_CONTRACT" : this.Text;
        }

        private void InitializeComponent()
        {
            base.SuspendLayout();
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x245, 0x1bd);
            base.FormBorderStyle = FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "xfmContractAdd";
            base.ShowIcon = false;
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Hợp Đồng Lao Động";
            base.ResumeLayout(false);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    base.Close();
                    return true;

                case Keys.F2:
                    this.ucAdd.IsClose = CloseOrNew.Close;
                    this.ucAdd.Save();
                    return true;

                case (Keys.Control | Keys.S):
                    this.ucAdd.IsClose = CloseOrNew.Close;
                    this.ucAdd.Save();
                    return true;

                case (Keys.Control | Keys.W):
                    this.ucAdd.IsClose = CloseOrNew.Close;
                    this.ucAdd.Save();
                    return true;

                case (Keys.Control | Keys.Shift | Keys.S):
                    this.ucAdd.IsClose = CloseOrNew.New;
                    this.ucAdd.Save();
                    return true;

                case (Keys.Alt | Keys.D):
                    return true;

                case (Keys.Alt | Keys.X):
                    base.Close();
                    return true;
            }
            return false;
        }

        public void RaiseAddedEventHander(HRM_CONTRACT Item)
        {
            if (this.Added != null)
            {
                this.Added(this, Item);
            }
        }

        public void RaiseUpdatedEventHander(HRM_CONTRACT Item)
        {
            if (this.Updated != null)
            {
                this.Updated(this, Item);
            }
        }

        private void ucAdd_CancelClick(object sender)
        {
            base.Close();
        }

        private void ucAdd_Success(object sender, HRM_CONTRACT Item)
        {
            if (this.ucAdd.Status == Actions.Add)
            {
                this.RaiseAddedEventHander(Item);
            }
            else if (this.ucAdd.Status == Actions.Update)
            {
                this.RaiseUpdatedEventHander(Item);
            }
            if (this.ucAdd.IsClose == CloseOrNew.Close)
            {
                base.Close();
            }
            this.ucAdd.Clear();
        }

        public delegate void AddedEventHander(object sender, HRM_CONTRACT Item);

        public delegate void UpdatedEventHander(object sender, HRM_CONTRACT Item);
    }
}

